# Frequency analysis is a method of breaking certain ciphers which involves counting the frequency of letters/symbols
# because English has quite a clear distribution of letters, with spikes on common letters such as 'e', 'i' and 's'

# Ask the user for a filename
filename = input("Please input a filename")
# open and read the file 
letter_counts = {}
try:
    with open(filename,"r")as file:
        content = file.read()
        

# and count how many instances of each letter are in the program
        for char in content:
            if char.isalpha():
                char_lower = char.lower()
                if char_lower in letter_counts:
                    letter_counts[char_lower] += 1
                else:
                    letter_counts[char_lower] = 1
    print("\nLetter counts:")
    for letter in sorted(letter_counts.keys()):  # Sort alphabetically
        print(f"{letter}: {letter_counts[letter]}")

except FileNotFoundError:
    print(f"Error: The file '{filename}' was not found.")
except Exception as e:
    print(f"An error occurred: {e}")
    

# hint: use a dictionary!

# Additional hints:
# - Remember to handle the case where the file might not exist
# - You'll need to check each character to see if it's a letter
# - Dictionary pattern: if key exists, increment; if not, set to 1
# - Consider converting to lowercase for consistent counting